package NC;

import java.util.Arrays;
import java.util.Comparator;

public class NC55 {
    public static String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0) return "";
        Arrays.sort(strs, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });
        for (int i = 1; i < strs.length; i ++ ) {
            if(strs[i].startsWith(strs[0])) continue;
            else {
                for (int j = 0; j < strs[0].length(); j ++ ) {
                    if(strs[0].charAt(j) != strs[i].charAt(j)) {
                        strs[0] = strs[0].substring(0, j);
                        break;
                    }
                }
            }
        }
        return strs[0];
    }
}
